package com.theobserver.theobserver;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;

public class UserNameModifyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_name_modify);
        final Sensitivewords sen = new Sensitivewords();

        Button bt=(Button)findViewById(R.id.button_confirm);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String t_str=((EditText)findViewById(R.id.editText_username)).getText().toString();

                //将文本框中的数据与敏感词库进行对比，返回值为0，则可以使用该用户名，不可以则返回1；
                int tmp = 0;

                for (int i=0;i< sen.str.length;i++){
                    if (t_str.matches(sen.str[i])){
                        Toast.makeText(UserNameModifyActivity.this, "含有不合乎规范的词汇，请重新输入！", Toast.LENGTH_SHORT).show();
                        tmp = 1;
                        break;
                    }
                }
                //将文本框中数据上传到当前用户的数据表中


                if(tmp == 0)
                {
                    AVUser c_user= AVUser.getCurrentUser();
                    c_user.put("user_name",t_str);
                    c_user.saveInBackground(new SaveCallback() {
                        @Override
                        public void done(AVException e) {
                            if(e == null)
                            {
                                Toast.makeText(UserNameModifyActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                                setResult(Code.STATECHANGE,null);
                                finish();
                            }
                        }
                    });
                }


            }
        });
        //对返回按钮设置监听
        TextView tv=(TextView)findViewById(R.id.textView_return);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserNameModifyActivity.this.finish();
            }
        });




    }
}

